{
 "cells": [
  {
   "metadata": {
    "collapsed": true
   },
   "cell_type": "markdown",
   "source": [
    "#### 少量样本示例的提示词模板\n",
    "    在构建prompt时，可以通过构建一个少量示例列表去进一步格式化prompt，这是一种简单但强大的指导生成方式，在某些情况下可以显著提高模型性能。\n",
    "    少量示例提示模板可以由一组示例或一个负责从定义的集合中选择一部分示例的示例选择器构建。\n",
    "    前者：使用FewShotPromptTemplate或FewShotChatMessagePromptTemplate\n",
    "    后者：使用Example selectors(示例选择器)\n",
    "    每个示例的结构都是一个字典，其中键是输入变量，值是输入变量的值\n",
    "    FewShotPromptTemplate：与PromptTemplate一起使用\n",
    "    FewShotChatMessagePromptTemplate：与ChatPromptTemplate一起使用"
   ],
   "id": "4d08489e59e7c2d0"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "##### FewShotPromptTemplate使用\n",
    "未提供示例的情况"
   ],
   "id": "470f36009d9df257"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "chat_model = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0.4)\n",
    "res = chat_model.invoke(\"2 鸟 9 是多少?\")\n",
    "print(res)"
   ],
   "id": "68b91d083c5201f6",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 提供少量示例情况",
   "id": "67917dfc2094879c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate\n",
    "\n",
    "examples = [\n",
    "    {\"input\": \"北京天气怎么样\", \"output\": \"北京市\"},\n",
    "    {\"input\": \"南京下雨吗\", \"output\": \"南京市\"},\n",
    "    {\"input\": \"武汉热吗\", \"output\": \"武汉市\"},\n",
    "\n",
    "]\n",
    "# 创建PromptTemplate实例\n",
    "template = PromptTemplate.from_template(template=\"input:{input}\\noutput:{output}\")\n",
    "\n",
    "few_shot_prompt_template = FewShotPromptTemplate(example_prompt=template, examples=examples,\n",
    "                                                 suffix=\"input:{input}\\noutput:\",\n",
    "                                                 input_variables=[\"input\"])\n",
    "\n",
    "prompt_value = few_shot_prompt_template.invoke({\"input\": \"天津会下雨吗?\"})\n",
    "response = chat_model.invoke(prompt_value)\n",
    "print(response.content)"
   ],
   "id": "c9317f4cb1d8fa9c",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "##### FewShotChatMessagePromptTemplate使用\n",
    "    特点：\n",
    "        自动将示例格式化为聊天消息(HumanMessage/AIMessage等)\n",
    "        输出结构化聊天消息(List[BaseMessage])\n",
    "        保留对话轮次结构"
   ],
   "id": "11e9cd661c7fca03"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "49a2ed6ebb0aa96"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
